//  我的-部门管理-成员补录
const app = getApp();
const db = wx.cloud.database();
const baseUrl = 'https://gsdse.cn/api/KJRecruit'

Page({
  data: {
    Department: ['秘书处', '网络研发部', "影视部", '动画部', "PC技术部", "新媒体运营部", 'UI设计部'], //部门名称
    position: ['主席', '副主席', '正部长', '副部长', '干事'], //职位名称
    departmentName: '请选择部门', //被选择的部门名称
    positionName: '请选择职位', //被选择的部门职位
    position_index: '', //职位索引
    department_index: '', //部门索引
    _openid: '',
    userId: ''
  },
  // ----------------------------------
  // methods
  write(e) {
    // this指向的是Page实例对象
    // console.log(e);
    let value = e.detail.value;
    this.setData({
      userId: value
    });
    // console.log(this.data._openid);
  },
  inputopen_id(e) {
    let userId = this.data.userId;
    // console.log(this.data._openid);
    if (this.data.userId == 0) {
      wx.showToast({
        icon: 'none',
        title: 'userId不能为空',
      });
      return false
    }
  },

  _ChooseDepartment(e) {
    // console.log(e);
    this.setData({
      department_index: parseInt(e.detail.value)  + 1,
    });
    this.setData({
      departmentName: this.data.Department[this.data.department_index - 1],
    })

  },
  _ChoosePosition(e) {
    // console.log(e);
    this.setData({
      positionName: '',
      position_index: e.detail.value,
    });
    this.setData({
      Position: this.data.position[this.data.position_index],
    });
  },

  async save(e) {
    if (this.data.Position == '' || this.data.department_index == 0 || this.data.userId == '') {
      wx.showToast({
        icon: "none",
        title: '请把信息填写完整',
      });
      // console.log('this.data.department_index',this.data.department_index)
      // console.log('this.data.Position',this.data.Position)
    } else {
      const userInfo = await getApp().selectUser(parseInt(this.data.userId))
      const updateData = {
        userId: parseInt(this.data.userId),
        applyId: userInfo.applyId,
        userName: userInfo.userName,
        phoneNumber: userInfo.phoneNumber,
        email: userInfo.email,
        positionName: this.data.Position != '干事' ? 1 : 0,
        position: this.data.Position,
        departmentId: parseInt(this.data.department_index),
        schoolClass: userInfo.schoolClass,
        wxuserId: userInfo.wxuserId,
      }
      getApp().updateUserInfo(updateData)
    }
  },

  //先保存用户现有的信息
  stroageUserInfo(userId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + '/selectUser',
        method: 'POST',
        data: {
          userId: userId
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.data.data.userId) {
            resolve(res.data.data)
          }
        },
        fail: function (res) {
          this.showError()
          console.log(res.data)
        }
      })
    })
  },

  //将用户信息写入数据库中
  updateUserInfo(userInfo) {
    return new Promise((resolve, reject) => {
      if (position === '正部长' || position === '副部长') {
        var positionName = 1
      } else {
        var positionName = 0
      }
      if (userInfo.schoolClass === null) {
        userInfo.schoolClass = ''
      }
      console.log({
        userId:userInfo.userId,
        applyId:userInfo.applyId,
        wxuserId:userInfo.userId,
        phoneNumber:userInfo.phoneNumber,
        email:userInfo.email,
        positionName:userInfo.positionName,
        position:userInfo.position,
        departmentId:userInfo.departmentId,
        schoolClass:userInfo.schoolClass,
        userName:userInfo.userName,
      })
      wx.request({
        url: baseUrl + '/updateUser',
        method: 'POST',
          data: {
            userId:userInfo.userId,
            applyId:userInfo.applyId,
            wxuserId:userInfo.userId,
            phoneNumber:userInfo.phoneNumber,
            email:userInfo.email,
            positionName:userInfo.positionName,
            position:userInfo.position,
            departmentId:userInfo.departmentId,
            schoolClass:userInfo.schoolClass,
            userName:userInfo.userName,
          },
          header: {
            "Content-Type": "application/json"
          },
          success:function(res) {
             resolve(true)
             wx.showToast({
               title: '修改成功',
               icon:'success'
             })
          }
      })
    })
  },

  //显示提交错误
  showError() {
    wx.showToast({
      title: '提交失败！',
      icon: 'error'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 深拷贝与浅拷贝
    //这里的业务场景 如果用了浅拷贝，就比较好！
    // this.setData({
    //   personInfo: app.personInfo
    // })
    // console.log(app.personInfo);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})